package com.android.djlmapp.activity.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.djlmapp.R;
import com.android.djlmapp.activity.home.web.GongGaoDetailActivity;
import com.android.djlmapp.adapter.home.GongGaoListAdapter;
import com.android.djlmapp.application.MyApplication;
import com.android.djlmapp.base.BaseActivity;
import com.android.djlmapp.model.home.GongGaoInfo;
import com.android.djlmapp.tools.global.ToastUtils;
import com.google.gson.Gson;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.refresh_view.pullrecyclerview.PullRecyclerView;
import org.refresh_view.pullrecyclerview.layoutmanager.XLinearLayoutManager;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GongGaoListActivity extends BaseActivity {

    @BindView(R.id.top_bac)
    ImageView topBac;
    @BindView(R.id.ll_top_back)
    LinearLayout llTopBack;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.home_iv_readpoint)
    ImageView homeIvReadpoint;
    @BindView(R.id.top_notes)
    ImageView topNotes;
    @BindView(R.id.iv_top_msg)
    ConstraintLayout ivTopMsg;
    @BindView(R.id.iv_top_set)
    ImageView ivTopSet;
    @BindView(R.id.iv_top_kf)
    ImageView ivTopKf;
    @BindView(R.id.tv_top_jump)
    TextView tvTopJump;
    @BindView(R.id.page_title_layout)
    ConstraintLayout pageTitleLayout;
    @BindView(R.id.pull_recycler_view)
    PullRecyclerView pullRecyclerView;
    @BindView(R.id.null_data_layout_img)
    ImageView nullDataLayoutImg;
    @BindView(R.id.null_data_layout_txt)
    TextView nullDataLayoutTxt;
    @BindView(R.id.null_data_layout)
    RelativeLayout nullDataLayout;

    private int page = 1;
    private GongGaoListAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gong_gao_list);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    @Override
    protected void initView() {
        llTopBack.setVisibility(View.VISIBLE);
        ivTopMsg.setVisibility(View.GONE);
        ivTopSet.setVisibility(View.GONE);
        ivTopKf.setVisibility(View.VISIBLE);
        tvName.setText("公告列表");
    }

    @Override
    protected void initData() {
        mAdapter = new GongGaoListAdapter(GongGaoListActivity.this);
        mAdapter.setOnItemClickListener(new GongGaoListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(GongGaoInfo.ListBean item) {
                // 跳转公告web界面
                startActivity(new Intent(GongGaoListActivity.this, GongGaoDetailActivity.class)
                        .putExtra("GGURL", item.getGonggao_url())
                        .putExtra("CONTENT_ID", item.getContent_id()));

            }
        });
        pullRecyclerView.setLayoutManager(new XLinearLayoutManager(this));
        pullRecyclerView.setColorSchemeResources(R.color.colorAccent);
        pullRecyclerView.setAdapter(mAdapter);
        pullRecyclerView.enableLoadMore(true);
        pullRecyclerView.setOnRecyclerRefreshListener(new PullRecyclerView.OnRecyclerRefreshListener() {
            @Override
            public void onPullRefresh() {
                page = 1;
                getData();
            }

            @Override
            public void onLoadMore() {
                page++;
                getData();
            }
        });
        pullRecyclerView.autoRefresh();

    }

    private void getData() {
        MyApplication.getInstance().getHttpTools().getGongGaoData(page + "", new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                Log.e("项目异常", "------跳转最新消息------" + response.body());
                try {
                    parseJson(response.body());
                } catch (Exception e) {
                    e.printStackTrace();
                    ToastUtils.showShortToast(GongGaoListActivity.this, "数据解析异常");
                    noData();
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                ToastUtils.showShortToast(GongGaoListActivity.this, "网络或服务器错误");
                noData();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                if (pullRecyclerView != null) {
                    pullRecyclerView.stopLoadMore();
                    pullRecyclerView.stopRefresh();
                }
            }
        });
    }

    private void parseJson(String json) {
        Gson gson = new Gson();
        GongGaoInfo info = gson.fromJson(json, GongGaoInfo.class);
        if (info != null) {

            if (info.getCode() == 200) {
                List<GongGaoInfo.ListBean> list = info.getList();
                if (list != null && list.size() == 20) {
                    if (nullDataLayout.getVisibility() == View.VISIBLE) {
                        nullDataLayout.setVisibility(View.GONE);
                    }
                    if (page == 1) {
                        mAdapter.clear();
                    }
                    mAdapter.addAll(list);
                } else {
                    ToastUtils.showShortToast(GongGaoListActivity.this, "未获取到更多数据");
                    noData();
                }
            } else {
                ToastUtils.showShortToast(GongGaoListActivity.this, info.getMsg());
                noData();
            }
        } else {
            ToastUtils.showShortToast(GongGaoListActivity.this, "返回数据为空");
            noData();
        }
    }

    /**
     * 无数据处理
     */
    private void noData() {
        if (page > 1) {
            page--;
        } else {
            mAdapter.clear();
            if (nullDataLayout.getVisibility() == View.GONE) {
                nullDataLayout.setVisibility(View.VISIBLE);
            }
        }
    }
}
